<h1><?php echo dl_lang_general("main.messages"); ?></h1>

<?php
setMessages();
?>

<button style="float: right" onclick="window.location='<?php echo $base; ?>messages/new';"><?php echo dl_lang_general("main.new_message"); ?></button>
<div style="clear: both"></div>
<?php
$s = dibi::query("SELECT m.id, a.name, m.date, m.mess_read, m.title, m.date
                    FROM account a, account_messages m
                   WHERE a.id = m.mess_from
                     AND m.mess_to = %i", $data["user"]["id"],
               "ORDER BY date DESC");
$r = $s->fetchAll();

$messages = array();
foreach ($r AS $v)
{
    $messages[$v->id] = array("from"  => $v->name,
                              "title" => $v->title,
                              "read"  => $v->mess_read,
                              "date"  => $v->date);
}

if (count($messages) > 0)
{
    echo '<br />
          <form method="post" action="" name="messagesForm">
          <input type="hidden" name="xfunction" value="xdeleteMessage">';
    echo '<table width="100%" rules="rows" style="border-top: 1px solid grey; border-bottom: 1px solid grey;">
            <col width="16">
            <col width="20">
            <col width="200">
            <col width="300">
            <col width="150" style="text-align: right">';
    foreach ($messages AS $id => $m)
    {
        ?>
            <tr style="cursor: pointer; <?php echo ($m["read"] == 0 ? "font-weight: bold;" : ""); ?>">
                <td><?php echo form_checkbox("messages", "", $id); ?></td>
                <td><?php echo html_image("images/messages/".($m["read"] == 1 ? "ico_read" : "ico_unread").".png", ($m["read"] == 1 ? dl_lang_general("main.message_read") : dl_lang_general("main.message_unread"))) ?></td>
                <td onclick="window.location='<?php echo $base."messages/".$id; ?>';"><?php echo $m["from"]; ?></td>
                <td onclick="window.location='<?php echo $base."messages/".$id; ?>';"><?php echo $m["title"]; ?></td>
                <td onclick="window.location='<?php echo $base."messages/".$id; ?>';"><?php echo date("d.m.Y H:i", $m["date"]); ?></td>
            </tr>    
        <?php
    }
    echo '</table><br />';
    echo '<button onclick="return confirm(\''.dl_lang_general("msg.messages_delete").'\')">'.dl_lang_general("main.messages_delete").'</button> <button onclick="if (confirm(\''.dl_lang_general("msg.messages_delete_all").'\')) window.location.href=\''.$base.'xdeleteMessage/all\'; return false;">'.dl_lang_general("main.messages_delete_all").'</button>';
    echo '</form>';
}
else
{
    echo "<br />".html_info(dl_lang_general("msg.no_message"));
}
?>